package com.homeworkh.usermanagementserver.algorithm;


import java.util.Arrays;
import java.util.Comparator;

/**
 * @author lq
 * @date 2021/1/16 14:27
 */
public class ShowComparator {

    public static class Student{
        private String name;
        private int age;
        private int id;

        public Student(String name,int age,int id){
            this.name = name;
            this.age = age;
            this.id = id;
        }
    }
    public static class IdComparator implements Comparator<Student>{

        @Override
        public int compare(Student o1, Student o2) {
            if (o1.id<o2.id){
                return 1;
            }else if (o2.id<o1.id){
                return -1;
            }else {
                return 0;
            }
        }
    }
    public static void printArray(int[] arr){
        for (int i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    }

    public static void printStudent(Student[] students){
        for (int i=0;i<students.length;i++){
            System.out.println(students[i].name+","+students[i].age+","+students[i].id);
        }
    }
    public static void main(String[] args) {
        int[] arr = {1,2,4,3,5,6,8,7,9};
        Arrays.sort(arr);
        printArray(arr);

        Student s1 = new Student("张三",14,1);
        Student s2 = new Student("李四",20,5);
        Student s3 = new Student("王五",18,6);
        Student s4 = new Student("赵六",16,9);
        Student s5 = new Student("李二",15,7);

        Student[] students = {s1,s2,s3,s4,s5};
        Arrays.sort(students,new IdComparator());
        printStudent(students);
    }
}
